//
// Contributed by TheBrodieMan
// Sole purpose of this QB is for obtaining a shredder mount from Azshara before it's available in the quest start area.
// It will target, move to, pull, and kill mob, then attempt to mount it.
// Once mounted, behaviour ends. Wrap in a "!Me.IsOnTransport" while loop for best results.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using CommonBehaviors.Actions;
using System.Windows.Forms;
using System.Windows.Media;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Profiles;
using Styx.Pathing;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

using Action = Styx.TreeSharp.Action;

namespace Honorbuddy.Quest_Behaviors.SpecificQuests.KillShredder
{
	[CustomBehaviorFileName(@"KillShredder")]
	public class KillShredder : CustomForcedBehavior
	{
		public KillShredder(Dictionary<string, string> args)
			: base(args)
		{
			try
			{
			}
			catch
			{
			}
		}
		private bool _isBehaviorDone = false;
		private Composite _root;

		public static void BPdiag(string message, params object[] args) { Logging.WriteDiagnostic(Colors.LawnGreen, "[QB DIAG TBM: " + message, args); }
		
		public override bool IsDone
		{
			get
			{
				return _isBehaviorDone;
			}
		}
		
		private LocalPlayer Me
		{
			get { return (StyxWoW.Me); }
		}
		
		private bool InVehicle
		{
			get { return Me.IsOnTransport; }
		}

		public override void OnStart()
		{
			OnStart_HandleAttributeProblem();
		}

		public WoWUnit Shredder
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWUnit>().Where(
					u => u.Entry == 35111 && u.Distance < 150 && !Blacklist.Contains(u.Guid, BlacklistFlags.Interact))
					.OrderBy(u => u.Distance).FirstOrDefault();
			}
		}

		public Composite DoneYet
		{
			get
			{
				return
					new Decorator(ret => InVehicle, new Action(delegate
					{
						TreeRoot.StatusText = "Finished!";
						_isBehaviorDone = true;
						return RunStatus.Success;
					}));
			}
		}

		public Composite ShredderKill
		{
			get
			{
				return new PrioritySelector(
					new Decorator(context => Shredder != null,
						new PrioritySelector(
							new Decorator(context => !Shredder.WithinInteractRange,
								new Action(context => { Navigator.MoveTo(Shredder.Location); })),
							new Decorator(context => Me.IsMoving,
								new Action(context => { WoWMovement.MoveStop(); })),
							new Decorator(context => Shredder.CurrentHealth > 10 && !Me.Combat,
								new Action(context => { Lua.DoString("StartAttack()"); })),
							new Decorator(context => Shredder.CurrentHealth < 10 && !Me.Combat,
								new Action(context => { Shredder.Interact(); }))
					)));
			}
		}

		protected override Composite CreateBehavior()
		{
			return _root ?? (_root = new Decorator(ret => !_isBehaviorDone,
				new PrioritySelector(
					new Decorator(context => InVehicle,
						new Action(context => { _isBehaviorDone = true; })),
					DoneYet, ShredderKill, new ActionAlwaysSucceed())));
		}
	}
}